<template>
    <div>
        <el-dialog center :before-close="close" :title="info.win ? '添加轮播图' : '修改轮播图'" :visible.sync="info.isShow">
            <el-form :model="bannerInfo" :rules="rules" ref="bannerForm">
                <el-form-item label="标题" prop="title" :label-width="formLabelWidth">
                    <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload action="#" list-type="picture-card" :on-preview="onPreview" :on-remove="handleRemove"
                        :on-change="changeFile" :file-list="fileList" :auto-upload="false" :limit="1"
                        :on-exceed="handleExceed">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="bannerInfo.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { bannerAdd, bannerInfo, bannerEdit } from '../../request/api'
// 辅助性函数
import { mapActions } from 'vuex'
export default {
    props: ['info'],
    data() {
        return {
            bannerInfo: {
                title: '', //轮播图名称
                img: '', //图片
                status: 1, //状态  1正常2禁用
            },
            rules: {
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' },
                    { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
                ],
            },
            formLabelWidth: '120px',
            dialogVisible: false, // 图片预览弹窗
            dialogImageUrl: '',  // 图片地址
            fileList: [], // 回显地址
        };
    },
    methods: {
        ...mapActions({
            bannerListAction: 'banner/bannerListAction'
        }),
        // 图片预览
        onPreview(file) {
            console.log(file, '图片预览');
            // 给预览图片地址赋值
            this.dialogImageUrl = file.url;
            // 打开预览弹窗
            this.dialogVisible = true;
        },
        // 图片删除
        handleRemove(file, fileList) {
            console.log(file, fileList, '图片删除');
        },
        // 修改图片时
        changeFile(file, fileList) {
            console.log(file, fileList, '上传成功');
            this.bannerInfo.img = file.raw
        },
        // 上传文件数量限制的提示
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        // 取消按钮事件
        close() {
            this.$emit('close', false)
            this.reset()
        },
        // 置空事件
        reset() {
            this.bannerInfo = {
                title: '', //轮播图名称
                img: '', //图片
                status: 1, //状态  1正常2禁用
            };
            // 清空校验
            this.$refs.bannerForm.resetFields()
            // 清空图片
            this.fileList=[] 
        },
        lookup(id) {
            // 调用查询接口
            bannerInfo({ id })
                .then(res => {
                    console.log(res, '一条数据');
                    this.bannerInfo = res.list
                    this.bannerInfo.id = id
                    this.fileList = this.bannerInfo.img ? [{ url: this.bannerInfo.img }] : []
                })
        },
        // 确认事件
        confirm() {
            this.$refs.bannerForm.validate(flag => {
                if (flag) {
                    // 验证成功
                    console.log(this.bannerInfo, '表单');
                    if (this.info.win) {
                        // 添加
                        bannerAdd(this.bannerInfo)
                            .then(res => {
                                console.log(res, '轮播图添加');
                                if (res.code == 200) {
                                    this.$message.success(res.msg)
                                    // 重新调用列表
                                    this.bannerListAction()
                                    // 清空弹窗
                                    this.close()
                                } else {
                                    this.$message.error(res.msg)
                                }
                            })
                    } else {
                        // 编辑
                        bannerEdit(this.bannerInfo)
                            .then(res => {
                                console.log(res, '轮播图编辑');
                                if (res.code == 200) {
                                    this.$message.success(res.msg)
                                    // 重新调用列表
                                    this.bannerListAction()
                                    // 清空弹窗
                                    this.close()
                                } else {
                                    this.$message.error(res.msg)
                                }
                            })
                    }


                } else {
                    // 验证失败
                }
            })
        }
    }
};
</script>

<style  lang="" scoped>

</style>
